#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};
    use std::thread;

    /// 测试Arc/Mutex在多线程环境中的安全共享
    /// 验证原子引用计数(Arc)和互斥锁(Mutex)的联合使用
    /// 演示所有权在线程间的安全转移机制
    #[test]
    fn test_arc_mutex() {
        let counter = Arc::new(Mutex::new(0));
        let mut handles = vec![];

        for _ in 0..10 {
            let counter = Arc::clone(&counter);
            let handle = thread::spawn(move || {
                let mut num = counter.lock().unwrap();
                *num += 1;
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(*counter.lock().unwrap(), 10);
    }

    /// 测试互斥锁异常传播机制
    /// 验证线程panic后锁的毒化(poisoning)现象
    /// 演示错误处理与锁状态恢复机制
    #[test]
    #[should_panic(expected = "already borrowed")]
    fn test_mutex_poisoning() {
        let lock = Arc::new(Mutex::new(0));
        let lock2 = Arc::clone(&lock);

        let _ = thread::spawn(move || {
            let _guard = lock2.lock().unwrap();
            panic!("测试互斥锁异常传播");
        }).join();

        let _ = lock.lock().unwrap();
    }

    /// 测试多生产者单消费者(mpsc)通道通信
    /// 验证线程间消息传递的所有权转移机制
    /// 演示Rust的通道内存安全保证
    #[test]
    fn test_channel() {
        use std::sync::mpsc;
        use std::thread;
        let (tx, rx) = mpsc::channel();
        thread::spawn(move || {
            let val = String::from("hello");
            tx.send(val).unwrap();
        });
        let received = rx.recv().unwrap();
        assert_eq!(received, "hello");
    }
}